package com.trkj.service.wxy;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wxy.QuestionBankDao;
import com.trkj.dao.wxy.QuestionTypeDao;
import com.trkj.mybatisdao.StaffMapper;
import com.trkj.pojo.QuestionBank;
import com.trkj.pojo.QuestionType;
import com.trkj.service.ServiceException;
import com.trkj.vo.wxy.QuestionTypePageVo;


@Service
@Transactional(rollbackFor = Exception.class)
public class QuestionTypeService {
	@Autowired
	private QuestionBankDao questionBankDao;
	@Autowired
	private QuestionTypeDao questionTypeDao;
	
	@Autowired 
	private StaffMapper staffMapper;
	/**
	 * 查询所有的记录
	 */
	public List<QuestionTypePageVo> queryByQuestionPage(int page,int rows,String questionType) throws ServiceException{
		try {
			Sort sort = new Sort(Direction.DESC,"testQuestions");
			List<Object[]> list = questionTypeDao.queryByQuestion(new PageRequest(page-1, rows,sort),questionType);
			List<QuestionTypePageVo> listQuestionPageVo = new ArrayList<>();
			if(list != null){

				for (Object[] objs : list) {
					QuestionType t = (QuestionType) objs[0];
					QuestionBank q = (QuestionBank) objs[1];
					
					QuestionTypePageVo questionTypePageVo = new QuestionTypePageVo();
					questionTypePageVo.setTestQuestions(t.getTestQuestions());
					questionTypePageVo.setQuesContent(t.getQuesContent());
					questionTypePageVo.setQuesDifficulty(t.getQuesDifficulty());
					questionTypePageVo.setNumberOptions(t.getNumberOptions());
					questionTypePageVo.setOptionsA(t.getOptionsA());
					questionTypePageVo.setOptionsB(t.getOptionsB());
					questionTypePageVo.setOptionsC(t.getOptionsC());
					questionTypePageVo.setOptionsD(t.getOptionsD());
					questionTypePageVo.setPictureMode(t.getPictureMode());
					questionTypePageVo.setQuesAnswer(t.getQuesAnswer());
					questionTypePageVo.setQuesExercise(t.getQuesExercise());
					questionTypePageVo.setQuesExam(t.getQuesExam());
					questionTypePageVo.setQuName(q.getQuName());
					
					listQuestionPageVo.add(questionTypePageVo);
				}
			}
			return listQuestionPageVo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}

	/**
	 * 查询所有的记录
	 */
	public List<QuestionTypePageVo> queryByQuestionTypePage(int page,int rows) throws ServiceException{
		try {
			List<Object[]> list = questionTypeDao.queryByQuestionType(new PageRequest(page-1, rows));
			
			List<QuestionTypePageVo> listQuestionPageVo = new ArrayList<>();
			if(list != null){
				for (Object[] objs : list) {
					QuestionType t = (QuestionType) objs[0];
					QuestionBank q = (QuestionBank) objs[1];
					
					QuestionTypePageVo questionTypePageVo = new QuestionTypePageVo();
					questionTypePageVo.setTestQuestions(t.getTestQuestions());
					questionTypePageVo.setQuesContent(t.getQuesContent());
					questionTypePageVo.setQuestionType(t.getQuestionType());
					questionTypePageVo.setQuesState(t.getQuesState());
					questionTypePageVo.setQuesDifficulty(t.getQuesDifficulty());
					questionTypePageVo.setNumberOptions(t.getNumberOptions());
					questionTypePageVo.setOptionsA(t.getOptionsA());
					questionTypePageVo.setOptionsB(t.getOptionsB());
					questionTypePageVo.setOptionsC(t.getOptionsC());
					questionTypePageVo.setOptionsD(t.getOptionsD());
					questionTypePageVo.setPictureMode(t.getPictureMode());
					questionTypePageVo.setQuesAnswer(t.getQuesAnswer());
					questionTypePageVo.setQuesExercise(t.getQuesExercise());
					questionTypePageVo.setQuesExam(t.getQuesExam());
					questionTypePageVo.setQuName(q.getQuName());
					
					listQuestionPageVo.add(questionTypePageVo);
				}
			}
			return listQuestionPageVo;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	
	/**
	 * 获取总记录数
	 * @return
	 */
	public long empByPageCount(){
		return questionTypeDao.count();
		
	}
	
	/**
	 * 新增
	 * @param quBank
	 * @param quType
	 * @throws ServiceException
	 */
	public void addMultipleInsert(QuestionBank quBank, QuestionType quType) throws ServiceException{
		try {
			quType.setTestQuestions(staffMapper.findOne()+"");
			quType.setQuestionBank(quBank);
			questionTypeDao.save(quType);
			QuestionBank qu=questionBankDao.findOne(quBank.getQuId());
			List<QuestionType> listquestionType = null;
			if(!qu.getQuestionTypes().isEmpty()){
				qu.getQuestionTypes().add(quType);
			}else{
				listquestionType = new ArrayList<>();
				listquestionType.add(quType);
			}
			qu.setQuestionTypes(listquestionType);
			questionBankDao.save(qu);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 删除
	 * @param testQuestions
	 * @throws ServiceException
	 */
	public void multiple_delete(String testQuestions) throws ServiceException{
		try {
			questionTypeDao.delete(testQuestions);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	
	}
	
	/**
	 * 单选编辑
	 * @param epId
	 * @param examPageVo
	 * @return
	 * @throws ServiceException
	 */
	public QuestionTypePageVo updateMultiple(String testQuestions,QuestionTypePageVo questionTypePageVo) throws ServiceException{
		
		try {
			QuestionType quType = questionTypeDao.queryByQuestionTestQuestions(testQuestions);
			QuestionBank questionBank = questionBankDao.findOne(quType.getQuestionBank().getQuId());
			
			questionTypePageVo.setTestQuestions(testQuestions);
			questionTypePageVo.setQuestionType(quType.getQuestionType());
			questionTypePageVo.setQuesDifficulty(quType.getQuesDifficulty());
			questionTypePageVo.setNumberOptions(quType.getNumberOptions());
			questionTypePageVo.setQuesExam(quType.getQuesExam());
			questionTypePageVo.setQuesExercise(quType.getQuesExercise());
			questionTypePageVo.setQuesContent(quType.getQuesContent());
			questionTypePageVo.setOptionsA(quType.getOptionsA());
			questionTypePageVo.setOptionsB(quType.getOptionsB());
			questionTypePageVo.setOptionsC(quType.getOptionsC());
			questionTypePageVo.setOptionsD(quType.getOptionsD());
			questionTypePageVo.setQuesAnswer(quType.getQuesAnswer());
			
			questionTypePageVo.setQuId(questionBank.getQuId());
			questionTypePageVo.setQuName(questionBank.getQuName());
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
		return questionTypePageVo;
		
	}
	
	/**
	 * 单选编辑之后保存
	 * @param epId
	 * @param examPageVo
	 * @throws ServiceException
	 */
	public void updateSavaMultipleSava(String testQuestions,QuestionTypePageVo questionTypePageVo) throws ServiceException{
		try {
			System.out.println(testQuestions);
			QuestionType quType = questionTypeDao.queryByQuestionTestQuestions(testQuestions);
			QuestionBank questionBank = questionBankDao.findOne(questionTypePageVo.getQuId());
			
			System.out.println("aaa"+ questionTypeDao.queryByQuestionTestQuestions(testQuestions));
			System.out.println(questionTypePageVo);
			
			quType.setTestQuestions(testQuestions);
			quType.setQuestionType(questionTypePageVo.getQuestionType());
			quType.setQuesDifficulty(questionTypePageVo.getQuesDifficulty());
			quType.setNumberOptions(questionTypePageVo.getNumberOptions());
			quType.setQuesExam(questionTypePageVo.getQuesExam());
			quType.setQuesExercise(questionTypePageVo.getQuesExercise());
			quType.setQuesContent(questionTypePageVo.getQuesContent());
			quType.setOptionsA(questionTypePageVo.getOptionsA());
			quType.setOptionsB(questionTypePageVo.getOptionsB());
			quType.setOptionsC(questionTypePageVo.getOptionsC());
			quType.setOptionsD(questionTypePageVo.getOptionsD());
			quType.setQuesAnswer(questionTypePageVo.getQuesAnswer());
			quType.setQuestionBank(questionBank);
			
			System.out.println("bbb"+ questionTypeDao.queryByQuestionTestQuestions(testQuestions));
			
			questionTypeDao.save(quType);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
}

